package dao.Impl;

import Util.DataUtil;
import bean.CClass;
import bean.TArea;
import bean.TInsect;
import dao.areaDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class areaDaoImpl implements areaDao {
    @Override
    public List<TArea> arealist() {
        List<TArea> list = new ArrayList<>();
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from t_area inner jion c_class where c_id = fk_classId";
        try {
            conet = DataUtil.getConn();
            ps = conet.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                TArea ta = new TArea();
                ta.setFAreaName(rs.getString("f_areaName"));
                ta.setFTreeType(rs.getString("f_treeType"));
                ta.setFLand(rs.getString("f_land"));
                ta.setFAdvTree(rs.getString("f_advTree"));
                CClass cClass = new CClass();
                cClass.setCId(rs.getLong("c_id"));
                cClass.setCName(rs.getString("c_name"));
                ta.setAClass(cClass);
                list.add(ta);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public List<TArea> ListByPage(int pageNo, int pageSize, String area) {
        List<TArea> list = new ArrayList<>();
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from t_area inner join c_class where t_area.fk_ClassId = c_class.c_id and 1=1";
        if(area!=null&&!area.equalsIgnoreCase("")){
            sql+=" and f_areaName like '%"+area+ "%'";

        }
        try {
            sql+=" limit " + (pageNo - 1) * pageSize + "," + pageSize;
            System.out.println(sql);
            conet = DataUtil.getConn();
            ps = conet.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                TArea ta = new TArea();
                ta.setFAreaName(rs.getString("f_areaName"));
                ta.setFTreeType(rs.getString("f_treeType"));
                ta.setFLand(rs.getString("f_land"));
                ta.setFAdvTree(rs.getString("f_advTree"));
                CClass cClass = new CClass();
                cClass.setCId(rs.getLong("c_id"));
                cClass.setCName(rs.getString("c_name"));
                ta.setClassName(rs.getString("c_name"));
                ta.setAClass(cClass);
                list.add(ta);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean add(TArea area) {
        int count = 0;
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean flag = false;
        try {
            conet= DataUtil.getConn();
            String sql = "insert into t_area(f_areaName,f_treeType,f_land,f_advTree) value(?,?,?,?)";
            ps = conet.prepareStatement(sql);
            ps.setString(1,area.getFAreaName());
            ps.setString(2,area.getFTreeType());
            ps.setString(3,area.getFLand());
            ps.setString(4,area.getFTreeType());
            count = ps.executeUpdate();
            if(count!=0){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataUtil.close(rs,ps,conet);
        }
        return flag;
    }

    @Override
    public boolean delete(TArea area) {
        return false;
    }

    @Override
    public TArea find(TArea area) {
        return null;
    }

    @Override
    public int getListCount(String area) {
        int count = 0;
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conet= DataUtil.getConn();
            String sql = "select count(0) from t_area where 1=1";

            if(area!=null&&area.equalsIgnoreCase("")){
                sql+=" and insect like '%"+area+"%'";
            }

            ps = conet.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataUtil.close(rs,ps,conet);
        }
        System.out.println("count = " + count);
        return count;
    }
}
